package com.dingdang.controller;


import com.dingdang.data.Result;
import com.dingdang.dto.FenQiDTO;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.TbFenqi;
import com.dingdang.mapper.TbFenqiMapper;
import io.swagger.annotations.Api;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@Api("定时发送分期付款信息服务")
@EnableScheduling
public class TimeController
{
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private TbFenqiMapper fenqiMapper;

    @Scheduled(cron = "0 13/1 0 0 1/1 ? *")
    public void SendMesages() throws ParseException
    {

        //编写向发送短信服务发送应还金额和代还期数
        //搜索出所有的分期表数据
        List<TbFenqi> fenqiList = fenqiMapper.selectList(null);
        for (TbFenqi fqList:fenqiList)
        {
            if (fqList.getFenqiFenqishu()>0)
            {
                FenQiDTO fenQiDTO = new FenQiDTO();
                BeanUtils.copyProperties(fqList,fenQiDTO);
                smsGetInFo(fenQiDTO);

                //分期数减一
                fqList.setFenqiFenqishuRemaining(fqList.getFenqiFenqishuRemaining()-1);
                try
                {
                    fenqiMapper.updateById(fqList);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                return;
            }
        }


    }

    public void smsGetInFo(FenQiDTO fenQiDTO)
    {
        try
        {
            rocketMQTemplate.convertAndSend("sms-Topic",fenQiDTO);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
    }
}
